package com.wcity.manage.service.impl;

import com.wcity.manage.common.context.Constants;
import com.wcity.manage.common.enums.Code;
import com.wcity.manage.common.exception.CustomException;
import com.wcity.manage.dao.dal.model.PageModel;
import com.wcity.manage.dao.mapper.PropertyFixMapper;
import com.wcity.manage.dao.model.Block;
import com.wcity.manage.dao.model.Fix;
import com.wcity.manage.service.property.PropertyFixService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 物业报修管理模块
 * Created by zm on 2016/12/12.
 */
@Service
public class PropertyFixServiceImpl implements PropertyFixService {

    @Resource private PropertyFixMapper propertyFixMapper;

    /**
     * 分页查询表单数据
     * @param pageModel
     * @return
     */
    @Override
    public List<Fix> getPayTypeListPage(PageModel pageModel) {
        return propertyFixMapper.findListPage(pageModel);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public int fixDelect(String id,int STARUS) {
        Fix fix = new Fix();
        fix.setId(Integer.parseInt(id));
        fix.setRemark(STARUS);
        return propertyFixMapper.update( fix );
    }

    /**
     * 获取维修信息
     * @param id
     * @return
     */
    @Override
    public Fix getFixInfo(String id) {
        return propertyFixMapper.getFixInfo(id);
    }

    /**
     * 处理
     * @param fix
     * @return
     */
    @Override
    public int fixEdit(Fix fix) {
        return propertyFixMapper.update( fix ) ;
    }

    /**
     *  前端报修添加接口
     * @param blockName
     * @param name
     * @param addr
     * @param phone
     * @param areeType
     * @param images
     * @param content
     * @param blockId
     * @return
     */
    @Override
    public String fixAdd(String blockName, String name, String addr, String phone, String areeType, String images, String content, String blockId ,String regId) {

        Fix fix = new Fix();
        fix.setBlockName( blockName );
        fix.setName( name );
        fix.setAddr( addr );
        fix.setPhone( phone );
        fix.setAreeType( areeType );
        fix.setImages( images );
        fix.setContent( content );
        fix.setCreateTime( new Date());
        fix.setRegId( Integer.parseInt(regId) );
        fix.setStatus("未处理");
        try{
            Block blockInfo = propertyFixMapper.getUserId( blockId );
            if ( blockInfo == null) {
                return "用户不存在";
            }
            fix.setUserId( blockInfo.getUserId() );
            int success = propertyFixMapper.insert( fix );
            if (success == 1){
                return "提交成功";
            }
        }catch (Exception e){
            e.printStackTrace();
            return "更新失败，服务器异常";
        }
        return "";
    }

    /**
     * 获取我的报修信息列表
     * @param pageModel
     * @return
     */
    @Override
    public Map<String,Object> getIFixList(PageModel pageModel) {
        Map<String,Object> map = new HashMap<>();
        try{
            List<Fix> fixList = propertyFixMapper.findListPage(pageModel);
            map.put("fixList",fixList);
            map.put("retCode",Code.SUCCESS.getCodeNum());
            map.put("retDesc","请求成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("err","服务器异常");
        }
        return map;
    }

    /**
     * 删除维修记录
     * @param fixId
     * @param status
     * @return
     */
    @Override
    public Map<String, Object> delect(String fixId, int status) {
        Map<String,Object> map = new HashMap<>();
        Fix fix = new Fix();
        fix.setId( Integer.parseInt(fixId) );
        fix.setRemark( status );
        try {
            int success = propertyFixMapper.update( fix );
            if(success==1){
                map.put("retCode",Code.SUCCESS.getCodeNum());
                map.put("retDesc","删除成功");
            }else{
                map.put("retCode",Code.ERROR.getCodeNum());
                map.put("retDesc","删除失败");
            }
        }catch (Exception e) {
            e.printStackTrace();
            map.put("err","服务器异常");
        }
        return map;
    }
}
